package cn.deschen.search.index.adUnitEle;

import cn.deschen.biz.domain.entity.AdRegion;
import cn.deschen.biz.service.AdRegionService;
import cn.deschen.common.utils.SpringUtil;
import cn.deschen.search.index.IndexAware;
import cn.deschen.search.index.adUnitEle.adUnitCreative.AdUnitCreativeIndex;
import cn.deschen.search.index.adUnitEle.adUnitCreative.AdUnitCreativeObject;
import cn.deschen.search.index.adUnitEle.adUnitKeyword.AdUnitKeywordIndex;
import cn.deschen.search.index.adUnitEle.adUnitKeyword.AdUnitKeywordObject;
import cn.deschen.search.index.adUnitEle.adUnitRegion.AdUnitRegionIndex;
import cn.deschen.search.index.adUnitEle.adUnitRegion.AdUnitRegionObject;
import cn.deschen.search.index.adUnitEle.adUnitTag.AdUnitTagIndex;
import cn.deschen.search.index.adUnitEle.adUnitTag.AdUnitTagObject;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static cn.deschen.biz.constants.UnitEleConstants.EleType.*;

/**
 * @Author hanbin_chen
 * @Description 推广单元与元素索引
 * @DateTime 2021/6/7
 * @Version V1.0.0
 */
@Component
@EqualsAndHashCode
public class AdUnitEleIndex extends IndexAware<String, AdUnitEleObject> {


    public static final Map<String, AdUnitEleObject> storageMap = new ConcurrentHashMap<>();

    public AdUnitEleIndex() {
        super(storageMap);
    }

    public static final String KEY_SEPARATOR = ":";

    @Autowired
    private AdRegionService regionService;

    @Override
    public void update(String key, AdUnitEleObject value) {
        Long id = Long.valueOf(key.split(KEY_SEPARATOR)[1]);
        switch (value.getEleType()) {
            case TAG: {
                AdUnitTagObject object = new AdUnitTagObject();
                object.setId(id);
                object.setUnitId(value.getUnitId());
                object.setTagId(value.getEleId());
                object.setIsDel(value.getIsDel());
                SpringUtil.getBean(AdUnitTagIndex.class).update(id, object);
            }
            break;
            case KEYWORD: {
                AdUnitKeywordObject object = new AdUnitKeywordObject();
                object.setId(id);
                object.setUnitId(value.getUnitId());
                object.setKeywordId(value.getEleId());
                object.setIsDel(value.getIsDel());
                SpringUtil.getBean(AdUnitKeywordIndex.class).update(id, object);
            }
            break;
            case REGION: {
                AdUnitRegionObject object = new AdUnitRegionObject();
                object.setId(id);
                object.setUnitId(value.getUnitId());
                object.setRegionId(value.getEleId());
                object.setIsDel(value.getIsDel());
                AdRegion region = regionService.getById(value.getEleId());
                object.setProvince(region.getProvince());
                object.setCity(region.getCity());
                SpringUtil.getBean(AdUnitRegionIndex.class).update(id, object);
            }
            break;
            case CREATIVE: {
                AdUnitCreativeObject object = new AdUnitCreativeObject();
                object.setId(id);
                object.setUnitId(value.getUnitId());
                object.setCreativeId(value.getEleId());
                object.setIsDel(value.getIsDel());
                SpringUtil.getBean(AdUnitCreativeIndex.class).update(id, object);
            }
            break;
        }
    }

    @Override
    public void delete(String key) {
        String eleType = key.split(KEY_SEPARATOR)[0];
        Long id = Long.valueOf(key.split(KEY_SEPARATOR)[1]);
        switch (eleType) {
            case TAG: {
                SpringUtil.getBean(AdUnitTagIndex.class).delete(id);
            }
            break;
            case KEYWORD: {
                SpringUtil.getBean(AdUnitKeywordIndex.class).delete(id);
            }
            break;
            case REGION: {
                SpringUtil.getBean(AdUnitRegionIndex.class).delete(id);
            }
            break;
            case CREATIVE: {
                SpringUtil.getBean(AdUnitCreativeIndex.class).delete(id);
            }
            break;
        }
    }

    public static String generateKey(AdUnitEleObject unitEle) {
        return unitEle.getEleType() + KEY_SEPARATOR + unitEle.getEleId();
    }
}
